package com.wujilin.okra.activity;

import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_SEARCH;
import static com.wujilin.okra.Okra.REQUEST_OKRA;

import java.lang.reflect.Field;

import android.app.ActionBar;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import com.wujilin.okra.R;
import com.wujilin.okra.adapter.OkraListAdapter;
import com.wujilin.okra.loader.QueryCursorLoader;

public class SearchActivity extends BaseActivity implements AdapterView.OnItemClickListener, 
		SearchView.OnQueryTextListener, LoaderCallbacks<Cursor> {
	private ListView listView;
	private SearchView searchView;
	private String searchText;
	
	private static final String SQL = "select o.*,c.name category from objective o left join category c on o.category_id=c._id where o.content like ?";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		initActionBar();
		initView();
	}
	
	private void initActionBar() {
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false); 
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowCustomEnabled(true);  
		actionBar.setCustomView(R.layout.action_bar_search);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;
			default:
				return super.onOptionsItemSelected(item);
		}
		return true;
	}
	
	private void initView() {
		listView = (ListView) findViewById(R.id.list);
		listView.setOnItemClickListener(this);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		searchView = (SearchView) findViewById(R.id.search);
		Class<?> searchViewClass = searchView.getClass();
		try {
			Field field = searchViewClass.getDeclaredField("mSearchHintIcon");
			field.setAccessible(true);
			ImageView searchHintIcon = (ImageView) field.get(searchView);
			searchHintIcon.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
		} catch (Exception e) {
		}
		searchView.setOnQueryTextListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		onOkraSelected(id);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case REQUEST_OKRA:
				onQueryTextChange(searchText);
				break;
		}
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		return false;
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		if (!TextUtils.isEmpty(newText)) {
			searchText = newText;
			newText = "%" + newText + "%";
			Bundle args = new Bundle();
			args.putStringArray("sqlArgs", new String[] { newText });
			getLoaderManager().restartLoader(LOADER_OKRA_LIST_BY_SEARCH, args, this);
		} else {
			listView.setAdapter(null);
		}
		return true;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new QueryCursorLoader(this, SQL, args.getStringArray("sqlArgs"));
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		ListAdapter adapter = new OkraListAdapter(this, data, searchText);
		listView.setAdapter(adapter);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		listView.setAdapter(null);
	}
}
